package com.zxl.live.screen.call.tools;

import android.os.Binder;
import android.os.IBinder;

import java.lang.reflect.Method;

public class TelephoneHelper {

    /**
     * 挂断电话
     *
     * @Permission android.permission.CALL_PHONE
     * @Permission android.permission.READ_PHONE_STATE
     * @Permission android.permission.PROCESS_OUTGOING_CALLS
     */
    public static void killCall() {
        try {
            //ITelephony.Stub.asInterface(ServiceManager.getService(Context.TELEPHONY_SERVICE)).endCall();
            Class<?> telephonyClass = Class.forName("com.android.internal.telephony.ITelephony");
            Class<?> telephonyStubClass = telephonyClass.getClasses()[0];
            Class<?> serviceManagerClass = Class.forName("android.os.ServiceManager");
            Class<?> serviceManagerNativeClass = Class.forName("android.os.ServiceManagerNative");
            Method getService = serviceManagerClass.getMethod("getService", String.class);
            Method tempInterfaceMethod = serviceManagerNativeClass.getMethod("asInterface", IBinder.class);
            Binder tmpBinder = new Binder();
            tmpBinder.attachInterface(null, "fake");
            Object serviceManagerObject = tempInterfaceMethod.invoke(null, tmpBinder);
            IBinder retbinder = (IBinder) getService.invoke(serviceManagerObject, "phone");
            Method serviceMethod = telephonyStubClass.getMethod("asInterface", IBinder.class);
            Object telephonyObject = serviceMethod.invoke(null, retbinder);
            Method telephonyEndCall = telephonyClass.getMethod("endCall");
            telephonyEndCall.invoke(telephonyObject);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    /**
     * 接听电话，理论只对2.3以下有效，反正也尝试下吧，android.permission.MODIFY_PHONE_STATE 权限获取不了
     *
     * @Permission android.permission.CALL_PHONE
     * @Permission android.permission.READ_PHONE_STATE
     * @Permission android.permission.PROCESS_OUTGOING_CALLS
     * @permisson android.permission.MODIFY_PHONE_STATE
     */
    public static void answerCall() throws Exception {
        //ITelephony.Stub.asInterface(ServiceManager.getService(Context.TELEPHONY_SERVICE)).endCall();
        Class<?> telephonyClass = Class.forName("com.android.internal.telephony.ITelephony");
        Class<?> telephonyStubClass = telephonyClass.getClasses()[0];
        Class<?> serviceManagerClass = Class.forName("android.os.ServiceManager");
        Class<?> serviceManagerNativeClass = Class.forName("android.os.ServiceManagerNative");
        Method getService = // getDefaults[29];
                serviceManagerClass.getMethod("getService", String.class);
        Method tempInterfaceMethod = serviceManagerNativeClass.getMethod("asInterface", IBinder.class);
        Binder tmpBinder = new Binder();
        tmpBinder.attachInterface(null, "fake");
        Object serviceManagerObject = tempInterfaceMethod.invoke(null, tmpBinder);
        IBinder retbinder = (IBinder) getService.invoke(serviceManagerObject, "phone");
        Method serviceMethod = telephonyStubClass.getMethod("asInterface", IBinder.class);
        Object telephonyObject = serviceMethod.invoke(null, retbinder);
        Method telephonyEndCall = telephonyClass.getMethod("answerRingingCall");
        telephonyEndCall.invoke(telephonyObject);
    }


}
